package com.xyht.sca_s.student_manage_system.modules.rxRegistration.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.req.RxRegistrationChargeCategoryDetailAddReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.req.RxRegistrationChargeCategoryDetailReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.entity.req.RxRegistrationChargeCategoryReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistration.service.RxRegistrationChargeCategoryDetailService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 收费项子项 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/rxRegistration/chargeCategoryDetail")
@SecretBody
public class RxRegistrationChargeCategoryDetailController {
    @Resource
    private RxRegistrationChargeCategoryDetailService rxRegistrationChargeCategoryDetailService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @PostMapping("/add")
    @ApiModelProperty("添加子收费项")
    public ResponseResult addRxRegistrationChargeCategoryDetail(@RequestBody RxRegistrationChargeCategoryDetailReq categoryDetailReq){
        return rxRegistrationChargeCategoryDetailService.addRxRegistrationChargeCategoryDetail(categoryDetailReq);
    }
    @PostMapping("/update")
    @ApiModelProperty("修改子收费项（仅价格）")
    public ResponseResult updateRxRegistrationChargeCategoryDetail(@RequestBody RxRegistrationChargeCategoryDetailReq categoryDetailReq){
        return rxRegistrationChargeCategoryDetailService.updateRxRegistrationChargeCategoryDetail(categoryDetailReq);
    }
    @PostMapping("/delete")
    @ApiModelProperty("删除子收费项")
    public ResponseResult deleteRxRegistrationChargeCategoryDetail(@RequestBody RxRegistrationChargeCategoryDetailReq categoryDetailReq){
        return rxRegistrationChargeCategoryDetailService.deleteRxRegistrationChargeCategoryDetail(categoryDetailReq);
    }
    @PostMapping("/batchAdd")
    @ApiModelProperty("批量添加子收费项")
    public ResponseResult batchAddRxRegistrationChargeCategoryDetail(@RequestBody RxRegistrationChargeCategoryDetailAddReq categoryDetailAddReq, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = rxRegistrationChargeCategoryDetailService.batchAddRxRegistrationChargeCategoryDetail(categoryDetailAddReq,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }
    @GetMapping("/export")
    @ApiModelProperty("导出子收费项模板")
    public ResponseResult exportRxRegistrationChargeCategoryDetail(@RequestParam("type") Integer type, HttpServletResponse response){
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = rxRegistrationChargeCategoryDetailService.exportRxRegistrationChargeCategoryDetail(type,response);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @GetMapping("/list")
    @ApiModelProperty("获取子收费项列表")
    public ResponseResult getRxRegistrationChargeCategoryDetailList(@RequestParam(value = "searchStr", required = false) String searchStr,
                                                              @RequestParam(value = "chargeCategoryId") String chargeCategoryId,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        return rxRegistrationChargeCategoryDetailService.getRxRegistrationChargeCategoryDetailList(searchStr, chargeCategoryId, pageNum, pageSize);
    }




}

